package com.lucweb.dao.impl;

import com.lucweb.dao.CallDao;
import com.lucweb.entity.TaskCall;

import org.hibernate.SessionFactory;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Repository("callDao")
public class CallDaoImpl implements CallDao {


    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;


    @Override
    public List<TaskCall> callList(String para) {
        List<TaskCall> callList = (List<TaskCall>) sessionFactory.getCurrentSession().createQuery("FROM TaskCall where "+para).list();

        return callList;
    }

    @Override
    public void saveOrUpdate(TaskCall taskCall) {
        sessionFactory.getCurrentSession().saveOrUpdate(taskCall);

    }

    @Override
    public TaskCall getcallById(int taskId) {
        return (TaskCall) sessionFactory.getCurrentSession().get(TaskCall.class, taskId);
    }

    @Override
    public void deletecall(int taskId) {

        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskCall.class, taskId)
        );
    }

    @Override
    public List<TaskCall> showCall() {
        List<TaskCall> callList = (List<TaskCall>) sessionFactory.getCurrentSession().createQuery("FROM TaskCall").list();

        return callList;
    }

    @Override
    public int getmaxId() {
        Integer number = (Integer) sessionFactory.getCurrentSession().createSQLQuery("select auto_increment from " +
                "information_schema.tables where table_schema='firesystem' and table_name='task_call';")
                .addScalar("auto_increment", StandardBasicTypes.INTEGER).uniqueResult();
        return number;
    }
}
